import java.util.LinkedList;
import java.util.Queue;

public class QueueToStack {
    class MyStack {
        public Queue<Integer> queue1;
        public Queue<Integer> queue2;

        public MyStack() {
            queue1=new LinkedList<>();
            queue2=new LinkedList<>();

        }

        public void push(int x) {
            if(!queue1.isEmpty()){
                queue1.offer(x);
            }else if(!queue1.isEmpty()){
                queue2.offer(x);
            }else{
                queue1.offer(x);
            }

        }

        public int pop() {
            if(empty()){
                return -1;
            }else if(!queue1.isEmpty()){
                int size=queue1.size();
                for(int i=0;i<size-1;i++){
                    queue2.offer(queue1.poll());
                }
                return queue1.poll();
            }else{
                int size=queue2.size();
                for(int i=0;i<size-1;i++){
                    queue1.offer(queue2.poll());
                }
                return queue2.poll();
            }

        }

        public int top() {
            if(empty()){
                return -1;
            }else if(!queue1.isEmpty()){
                int size=queue1.size();
                for(int i=0;i<size-1;i++){
                    queue2.offer(queue1.poll());
                }
                int x=queue1.poll();
                queue2.offer(x);
                return x;
            }else{
                int size=queue2.size();
                for(int i=0;i<size-1;i++){
                    queue1.offer(queue2.poll());
                }
                int x=queue2.poll();
                queue1.offer(x);
                return x;
            }


        }

        public boolean empty() {
            return queue1.isEmpty()&&queue2.isEmpty();

        }
    }

/**
 * Your MyStack object will be instantiated and called as such:
 * MyStack obj = new MyStack();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.top();
 * boolean param_4 = obj.empty();
 */
}
